package com.cpi.cornpp.instructions.futures.services;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.instructions.futures.converter.InstructionsFuturesConverter;
import com.cpi.cornpp.instructions.futures.dao.InstructionsFuturesDAO;
import com.cpi.cornpp.instructions.futures.dto.InstructionsFuturesDTO;
import com.cpi.cornpp.instructions.futures.exception.InstructionsFuturesException;
import com.cpi.cornpp.instructions.futures.view.InstructionsFuturesView;
import com.cpi.cornpp.period.exception.PeriodException;
import com.cpi.cornpp.period.service.PeriodService;
import com.cpi.cornpp.period.view.PeriodView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;


/**
 * Instructions Futures Service
 * @author Edgar Joao
 *
 */
public class InstructionsFuturesService {
	private static final String PURCHASE_INSTRUCTION_FUTURES_PERIOD_YEARS = "PURCHASE_INSTRUCTION_FUTURES_PERIOD_YEARS";

	/**
	 * @author Edgar Joao
	 * @param id
	 * @return
	 * @throws InstructionsFuturesException
	 */
	
	
	public List getInstructionsFuturesList(int id) throws InstructionsFuturesException {
		InstructionsFuturesDAO dao = null;
		List list = null;
		List listDTOs = null;
		InstructionsFuturesConverter converter = null;
		list = new ArrayList(0);
		try{			
			dao = new InstructionsFuturesDAO();			
			converter = new InstructionsFuturesConverter();
			listDTOs = dao.getInstructionsFuturesList(id);
			Iterator it = listDTOs.iterator();
			while (it.hasNext()) {
				InstructionsFuturesDTO  dto = (InstructionsFuturesDTO ) it.next();
				InstructionsFuturesView view = converter.converterInstructionsFuturesDtoToView(dto,this.periodAccountApplication(),this.periodAccountApplication());
				list.add(view);
			}
			
													
		} catch (InstructionsFuturesException plantVersionException) {
			throw plantVersionException;
		} catch (Exception e){
			InstructionsFuturesException plantVersionException = null;
			e.printStackTrace(System.out);
			plantVersionException = new InstructionsFuturesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw plantVersionException;
		}	
		return list;
	}
	/**
	 * @author Edgar Joao
	 * @param dto
	 * @throws InstructionsFuturesException
	 */
	public void saveInstructionsFutures(List instructionsFutures) throws InstructionsFuturesException {
		Iterator iterator = null;
		InstructionsFuturesDAO dao = null;
		InstructionsFuturesConverter converter = null;
		InstructionsFuturesDTO dto = null;
		InstructionsFuturesView view = null;		
		try{
			iterator = instructionsFutures.iterator();
			dao = new InstructionsFuturesDAO();
			converter = new InstructionsFuturesConverter();
			//Validaciones para agregar, eliminar o actualizar
			//Regla Agregar: Id purchase_instruction_future_id(hidden) Si viene en 0 y los dos primeros vienen diferentes de 0[][]
			//Actualizar: Id purchase_instruction_future_id(hidden) Si viene > 0 y los dos primeros vienen diferentes de 0[][]
			//Borrar:Id purchase_instruction_future_id(hidden) Si viene > 0 y los dos primeros vienen igual a 0[][]
			while(iterator.hasNext()){
				view = (InstructionsFuturesView) iterator.next();
				dto = converter.converterInstructionsFuturesViewToDto(view);
				//Agregar	
				if((dto.getPurchaseInstructionFutureId() == 0 ) && (dto.getQuantity() != 0)){
					dao.insertInstructionsFutures(dto);
				} else if((dto.getPurchaseInstructionFutureId() > 0 ) && (dto.getQuantity() != 0)){
					//Actualizar
					dao.updateInstructionsFutures(dto);
				} else if((dto.getPurchaseInstructionFutureId() > 0)){
					//Eliminar
					dao.deleteInstructionsFutures(dto.getPurchaseInstructionFutureId());
				}
			}
			
		} catch (InstructionsFuturesException instructionsFuturesException) {
			throw instructionsFuturesException;
		} catch (Exception e){
			InstructionsFuturesException instructionsFuturesException = null;
			e.printStackTrace(System.out);
			instructionsFuturesException = new InstructionsFuturesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_UPDATE);
			throw instructionsFuturesException;
		}	
	}
	
	/**
	 * @author Edgar Joao
	 * @param dto
	 * @throws InstructionsFuturesException
	 */
	public void saveInstructionsFutures(List instructionsFutures, int purchaseInstructionDetailid) throws InstructionsFuturesException {
		for (Iterator iter = instructionsFutures.iterator(); iter.hasNext();) {
			InstructionsFuturesView view = (InstructionsFuturesView) iter.next();
			view.setPurchaseInstructionDetailId(purchaseInstructionDetailid);
		}
		this.saveInstructionsFutures(instructionsFutures);
	}
	
	public List periodAccountApplication() throws PeriodException {
		List result = null;
		PeriodService periodService = null;		
		Iterator iterator = null;
		PeriodView periodView = null;
		Calendar c = null;	
		periodService = new PeriodService();
		result = new ArrayList(0);
		c = Calendar.getInstance();
		SystemParameterCollection systemParameterCollection = null;
		SystemParameterBean systemParameterBean = null;
		int years = 0;
		try {
			systemParameterCollection = SystemParameterCollection.getInstance();
			systemParameterBean = systemParameterCollection.getSystemParameterBean(PURCHASE_INSTRUCTION_FUTURES_PERIOD_YEARS);
			years = systemParameterBean.getIntValue();
			iterator = periodService.getActivePeriods().iterator();
			while(iterator.hasNext()){ 
				periodView = (PeriodView) iterator.next();
				if( ( periodView.getPeriodYear() > c.get(Calendar.YEAR) ) || (periodView.getPeriodYear() == c.get(Calendar.YEAR) && periodView.getPeriodEndMonth() >= c.get(Calendar.MONTH) ) ){
					if( periodView.getPeriodYear()  <= ( c.get(Calendar.YEAR) + years ) ){
						result.add(periodView);
					}
				}
			}
		} catch (PeriodException periodException) {
			throw periodException;	
		} catch (Exception e) {
			PeriodException periodException = null;
			e.printStackTrace(System.out);
			periodException = new PeriodException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw periodException;
		}
		return result;
	}
	
	public void refreshPeriods(List instructionsFuturesView) {
		for (Iterator iter = instructionsFuturesView.iterator(); iter.hasNext();) {
			InstructionsFuturesView view = (InstructionsFuturesView) iter.next();
			for (Iterator iterator = view.getAccountPeriod().iterator(); iterator.hasNext();) {
				PeriodView period = (PeriodView) iterator.next();
				if(view.getAccountFuturePeriodId() == period.getPeriodId()){
					period.setSelected(true);
				}else{
					period.setSelected(false);
				}
			}
			for (Iterator iterator = view.getApplicationPeriod().iterator(); iterator.hasNext();) {
				PeriodView period = (PeriodView) iterator.next();
				if(view.getApplicationFuturePeriodId() == period.getPeriodId()){
					period.setSelected(true);
				}else{
					period.setSelected(false);
				}
			}
		}
	}
	
	public boolean closeInstructionDetail(int purchaseInstructionDetailId)throws InstructionsFuturesException {
		boolean closeInstructionDetail = false;
		InstructionsFuturesDAO instructionsFuturesDAO = null;
		int balance = 0;
		try{
			instructionsFuturesDAO = new InstructionsFuturesDAO();
			balance = instructionsFuturesDAO.instructionFuturesBalance(purchaseInstructionDetailId);
			if( balance <= 0 ){
				closeInstructionDetail = true;
			}
		} catch (InstructionsFuturesException instructionsFuturesException) {
			throw instructionsFuturesException;
		} catch (Exception e){
			InstructionsFuturesException instructionsFuturesException = null;
			e.printStackTrace(System.out);
			instructionsFuturesException = new InstructionsFuturesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_SELECT);
			throw instructionsFuturesException;
		}	
		return closeInstructionDetail;
	}
}
